package com.example.admin.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by admin on 2017/1/11.
 */

public class MChart extends View{
    private String TAG = "MChart";
    private ArrayList<Float> mMileages = new ArrayList<>();
    private ArrayList<Float> mTimes = new ArrayList<>();
    private ArrayList<Entry> mMileageDatas = new ArrayList<>();
    private ArrayList<String> mDates = new ArrayList<>();
    private ArrayList<Entry> mTimesDatas = new ArrayList<>();
//    private String[] dates = new String[]{"一","二","三","四","五","六","日"};
    private int mTotalWidth;
    private int mTotalHeight;
    private Paint mTimePaint = new Paint();
    private Paint mMileagePaint = new Paint();
    private Paint mTextPaint = new Paint();
    private Paint mLinePaint = new Paint();
    private Paint mXpointPaint = new Paint();

    private int mXcount;
    private float mXYpadding = 60;
    private float mPointRadius = 7f;
    private float mLinePadding = 10;

    @Override
    protected void onDraw(Canvas canvas) {
        //画坐标线
        canvas.drawLine(0,mTotalHeight-mXYpadding,mTotalWidth,mTotalHeight-mXYpadding,mLinePaint);
        //计算文字站位
        float rTextWidth = mTextPaint.measureText("100min");
        float lTextWidth = mTextPaint.measureText("100km");
        float xTextWidth = mTextPaint.measureText("一");
        //画Y坐标数字
        for(int i=0;i<mMileages.size();i++){
            canvas.drawText((int)(float)mMileages.get(i)+"km",0
                    ,getMileageY(mMileages.get(i))
                    ,mTextPaint);
            canvas.drawText((int)(float)mTimes.get(i)+"min"
                    ,mTotalWidth-rTextWidth,getTimeY(mTimes.get(i))
                    ,mTextPaint);
            //按里程划线
            canvas.drawLine(lTextWidth+mLinePadding,getMileageY(mMileages.get(i))-mTextPaint.getTextSize()/2
                    ,mTotalWidth-rTextWidth-mLinePadding,getMileageY(mMileages.get(i))-mTextPaint.getTextSize()/2,mLinePaint);
        }
        //画X坐标显示文字
        for(int i=0;i<mDates.size();i++){
            canvas.drawText(mDates.get(i),getTextX(i)-xTextWidth/2,mTotalHeight,mTextPaint);
            canvas.drawCircle(getTextX(i),mTotalHeight-mXYpadding,mPointRadius,mXpointPaint);
        }

        //画点，连线 里程
        for(int i=0;i<mMileageDatas.size();i++){
            canvas.drawCircle(getX(mMileageDatas.get(i).x),getMileageY(mMileageDatas.get(i).y),mPointRadius,mMileagePaint);
            if(i>0){
                canvas.drawLine(getX(mMileageDatas.get(i-1).x),getMileageY(mMileageDatas.get(i-1).y)
                        ,getX(mMileageDatas.get(i).x),getMileageY(mMileageDatas.get(i).y),mMileagePaint);
            }
        }
        //最后一段线
        canvas.drawLine(getX(mMileageDatas.get(mMileageDatas.size()-2).x),getMileageY(mMileageDatas.get(mMileageDatas.size()-2).y)
                ,getX(mMileageDatas.get(mMileageDatas.size()-1).x),getMileageY(mMileageDatas.get(mMileageDatas.size()-1).y),mMileagePaint);


        //画点，连线 时间
        for(int i=0;i<mTimesDatas.size();i++){
            canvas.drawCircle(getX(mTimesDatas.get(i).x),getTimeY(mTimesDatas.get(i).y),mPointRadius,mTimePaint);
            if(i>0){
                canvas.drawLine(getX(mTimesDatas.get(i-1).x),getTimeY(mTimesDatas.get(i-1).y)
                        ,getX(mTimesDatas.get(i).x),getTimeY(mTimesDatas.get(i).y),mTimePaint);
            }
        }
        //最后一段线
        canvas.drawLine(getX(mTimesDatas.get(mTimesDatas.size()-2).x),getTimeY(mTimesDatas.get(mTimesDatas.size()-2).y)
                ,getX(mTimesDatas.get(mTimesDatas.size()-1).x),getTimeY(mTimesDatas.get(mTimesDatas.size()-1).y),mTimePaint);
    }

    private float getX(float x){
        return (mTotalWidth - 2*mXYpadding)/(mXcount+1)*(x+1) + mXYpadding;
    }
    private float getTextX(int x){
        return (mTotalWidth - 2*mXYpadding)/(mDates.size()+1)*(x+1) + mXYpadding;
    }
    private float getMileageY(float mile){
        float maxY = mMileages.get(mMileages.size()-1);
        return (1-mile/maxY)*(mTotalHeight-2*mXYpadding) + mXYpadding + mTextPaint.getStrokeWidth();
    }
    private float getTimeY(float time){
        float maxY = mTimes.get(mTimes.size()-1);
        return (1-time/maxY)*(mTotalHeight-2*mXYpadding) + mXYpadding + mTextPaint.getStrokeWidth();
    }
    /**
     * 初始化
     */
    private void init(){
        mMileagePaint.setColor(Color.BLUE);
        mMileagePaint.setStrokeWidth(3);

        mTimePaint.setColor(Color.RED);
        mTimePaint.setStrokeWidth(3);

        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setStrokeWidth(5);
        mTextPaint.setTextSize(36);

        mLinePaint.setStrokeWidth(3);
        mLinePaint.setColor(Color.GRAY);

        mXpointPaint.setColor(Color.YELLOW);
    }
    public MChart(Context context) {
        super(context);
        init();
    }

    public MChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void setTextPaint(Paint paint){
        mTextPaint = paint;
    }
    public void setMileagePaint(Paint paint){
        mMileagePaint = paint;
    }
    public void setTimePaint(Paint paint){
        mTimePaint = paint;
    }
    public void setLintPaint(Paint paint){
        mLinePaint = paint;
    }

    /**
     * 设置里程纵坐标
     * 划线的条数根据list.size(),如果需要每条线之间距离相等,需要将最大值平分成几分设置到list中
     *
     */
    public void setMileageList(ArrayList<Float> list){
        mMileages = list;
    }

    /**
     * 设置时间纵坐标
     */
    public void setTimeList(ArrayList<Float> list){
        mTimes = list;
    }

    /**
     * 设置里程数据
     */
    public void setMileageDataList(ArrayList<Entry> list){
        mMileageDatas = list;
    }

    /**
     * 设置时间数据
     */
    public void setTimeDataList(ArrayList<Entry> list){
        mTimesDatas = list;
    }

    /**
     * 设置X轴显示时间，设置需要显示的最大的天数
     * @param list
     * @param count 最大天数
     */
    public void setDatesList(ArrayList<String> list,int count){
        mDates = list;
        mXcount = count;
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mTotalWidth = getWidth();
        mTotalHeight = getHeight();
    }
}
